﻿<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>前锋图书管理系统</title>
    <link rel="stylesheet" href="/css/public.css"/>
    <link rel="stylesheet" href="/css/style.css"/>
    <link rel="stylesheet" href="/js/layui/css/layui.css"  media="all">
</head>
<style>
    html,body{
        width: 100%;
        height: 100%;
    }
    .homeWrap{
        width: 100%;height: 100%;
        padding-top: 76px;position: relative;box-sizing: border-box;overflow: hidden;
    }
    .homeWrap div{box-sizing: border-box;}
    .publicMian,.left{height: 100%;}
    .homeHeader{width: 100%; position: absolute;left: 0;top: 0;}
</style>
<body>
<!--头部-->
<div class="homeWrap">
    <div class="homeHeader">
        <header class="publicHeader">
            <h1>前锋图书管理系统</h1>

            <div class="publicHeaderR">
                <p><span>下午好！</span><span style="color: #fff21b"> Admin</span> , 欢迎你！</p>
                <a href="/admin/index/signOut">退出</a>
            </div>
        </header>

        <!--时间-->
        <section class="publicTime">
            <span id="time">2015年1月1日 11:11  星期一</span>
        </section>
    </div>
<!--主体内容-->
<section class="publicMian ">
    <div class="left">
        <h2 class="leftH2"><span class="span1"></span>功能列表 <span></span></h2>
        <nav>
            <ul class="list">
                <li><a href="/admin/home/home">首页</a></li>
                <li><a href="/admin/home/borrowingmanagement">借阅管理</a></li>
                <li><a href="/admin/home/bookrecord">图书记录</a></li>
                <li id="active"><a href="/admin/home/bookadd">图书添加</a></li>
                <li><a href="/admin/scrollmember/scroll">滚屏</a></li>
                <li><a href="/admin/scrollmember/member">会员管理</a></li>
                <li><a href="/admin/home/editpass">修改密码</a></li>
                <li><a href="/admin/home/groupbuy">团队来源</a></li>
            </ul>
        </nav>
    </div>
    <div class="right">
        <div class="location">
            <strong>你现在所在的位置是:</strong>
            <span>图书添加 >> 添加图书页面</span>
        </div>
        <div class="providerAdd">

            <div style="width: 270px;height: 300px;border: 1px solid grey;margin-left: 430px;">
                <label for="billNum" style="margin-left:-200px;">图片：</label>
                <div>
                    <div class="layui-upload">
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="demo1" style="width: 100%;margin-top: -30px;height: 200px;">
                            <p id="demoText"></p>
                        </div>
                        <button type="button" class="layui-btn" id="test1" style="margin-left: 80px;">上传图片</button>
                    </div>
                </div>
                <span style="position: relative;top: 0px;left: 276px;">*图书封面</span>
            </div>
            <div style="margin-left: 226px;">
                <label for="money">书名：</label>
                <input type="text" name="money" id="money" required/>
                <span>*请填写图书名称</span>
            </div>
            <div style="margin-left: 226px;">
                <label >图书来源：</label>
                <input type="text" name="money" id="bookFrom" required/>
                <span>*请填写图书供应商</span>
            </div>

            <div class="providerAddBtn" style="margin-left: 438px;">
                <input type="button" value="添加" onclick="addBook()" style="width: 274px;margin-left: -10px;"/>
            </div>
        </div>

    </div>
</section>
 </div>
<script src="/js/jquery.js"></script>
<script src="/js/time.js"></script>
<script src="/js/layui/layui.js"></script>
<script>
    var imgUrl = '';   // 图片地址
    layui.use('upload', function(){
        var $ = layui.jquery
            ,upload = layui.upload;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: '/admin/UploadImg/uploadImg'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                console.log(res)
                //如果上传成功
                if(res.code == 1){
                    imgUrl = res.photo;
                }
                else{
                    return layer.msg('上传失败');
                }
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
    });

    // 添加图书
    function addBook(){
        var bookName = $("#money").val();
        var bookFrom = $("#bookFrom").val();
        if(imgUrl == ''){
            layer.msg('请上传图书图片');
        }
        else if(bookName == ''){
            layer.msg('请填写图书名称');
        }
        else if(bookFrom == ''){
            layer.msg('请选择图书来源');
        }
        else{
            $.ajax({
                url:'/admin/home/bookAdd',
                type:'POST',
                data:{
                    imgUrl:imgUrl,
                    bookName:bookName,
                    bookFrom:bookFrom
                },
                success:function(res){
                    if(res == 1){
                        layer.msg('添加成功');
                        setTimeout(function(){
                            window.location.href = '/admin/home/bookadd'
                        },2000);
                        // window.location.href = '/admin/home/bookadd';
                    }
                    else if(res == 2){
                        layer.msg('本书已有，无需再次添加');
                    }
                    else{
                        layer.msg('添加失败');
                    }
                },
                error:function(){

                }
            });
        }

    }
</script>

</body>
</html>